package com.snfdf.camunda;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@SpringBootApplication
public class Application {

    private final RuntimeService runtimeService;
    private final IdentityService identityService;

    public static void main(String... args) {
        SpringApplication.run(Application.class, args);
    }

    // @PostConstruct
    public void userTask() {
        identityService.setAuthenticatedUserId("xiaobao");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Process_user_task");
        String processInstanceId = processInstance.getProcessInstanceId();
        log.info(processInstanceId);
    }

    // @PostConstruct
    public void multiTask() {
        identityService.setAuthenticatedUserId("xiaoming");

        VariableMap variables = Variables.createVariables();
        variables.put("leaders", List.of(
                "wangbing",
                "zhangsan",
                "wangwu"
        ));
        runtimeService.startProcessInstanceByKey("Process_multi_task", variables);
    }

    // @PostConstruct
    public void multiParallelTask() {
        identityService.setAuthenticatedUserId("xiaoming");

        VariableMap variables = Variables.createVariables();
        variables.put("leaders", List.of(
                "wangbing",
                "zhangsan",
                "wangwu"
        ));
        runtimeService.startProcessInstanceByKey("Process_multi_parallel_task", variables);
    }

    // @PostConstruct
    public void scriptTask() {
        identityService.setAuthenticatedUserId("xiaoming");
        VariableMap variables = Variables.createVariables();
        variables.put("originDays", 10);
        runtimeService.startProcessInstanceByKey("Process_script_task", variables);
    }

    // @PostConstruct
    public void externalScriptTask() {
        identityService.setAuthenticatedUserId("xiaoming");
        VariableMap variables = Variables.createVariables();
        variables.put("originDays", 10);
        runtimeService.startProcessInstanceByKey("Process_external_script_task", variables);
    }

    // @PostConstruct
    public void callActivitySubprocess() {
        identityService.setAuthenticatedUserId("xiaoming");
        VariableMap variables = Variables.createVariables();
        variables.put("videoNames", Arrays.asList("电影", "电视剧", "综艺节目"));
        runtimeService.startProcessInstanceByKey("Process_call_activity_main_subprocess", variables);
    }
}
